#!/bin/bash

source /etc/profile

workdir=$(cd $(dirname $0); pwd)
source $workdir/config.cfg

tomorrow=`date -d "+1 day " +%Y-%m-%d`
log_dir=$workdir/log
log_file=$workdir/log/execute-$today.log

# export data to mysql
tables=(${bi_export_tables//,/$'\n'})
for table in "${tables[@]}";
do
	echo "`date '+%Y-%m-%d %H:%M:%S'` -- export table $table to mysql  "
	sqoop eval --connect "jdbc:mysql://${bi_db_url}:${bi_db_port}/${bi_db}" --username ${bi_db_username} --password "${bi_db_password}" -e "truncate table t_$table"
	sqoop export -D mapred.job.queue.name=root.sync --connect "jdbc:mysql://${bi_db_url}:${bi_db_port}/${bi_db}" --username ${bi_db_username} --password "${bi_db_password}" --table t_$table \
		--fields-terminated-by '\001' --lines-terminated-by '\n' --export-dir "/user/hive/warehouse/bi_report.db/$table"
	if [ "$?" -ne "0" ]
	then
        	echo "execute sqoop error,table : ${table}" >> $log_dir/execute_result_$today
    	fi
done

sqoop eval --connect "jdbc:mysql://${bi_db_url}:${bi_db_port}/${bi_db}" --username ${bi_db_username} --password "${bi_db_password}" -e "insert into t_new_user_realtime_stat(stat_date) values ('$tomorrow')"
sqoop eval --connect "jdbc:mysql://${bi_db_url}:${bi_db_port}/${bi_db}" --username ${bi_db_username} --password "${bi_db_password}" -e "insert into t_today_realtime_stat_day(stat_date) values ('$tomorrow')"
sqoop eval --connect "jdbc:mysql://${bi_db_url}:${bi_db_port}/${bi_db}" --username ${bi_db_username} --password "${bi_db_password}" -e "insert into t_today_realtime_stat_hour(stat_date,stat_hour) values ('$tomorrow',0),('$tomorrow',1),('$tomorrow',2),('$tomorrow',3),('$tomorrow',4),('$tomorrow',5),('$tomorrow',6),('$tomorrow',7),('$tomorrow',8),('$tomorrow',9),('$tomorrow',10),('$tomorrow',11),('$tomorrow',12),('$tomorrow',13),('$tomorrow',14),('$tomorrow',15),('$tomorrow',16),('$tomorrow',17),('$tomorrow',18),('$tomorrow',19),('$tomorrow',20),('$tomorrow',21),('$tomorrow',22),('$tomorrow',23)"
